﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Foothill.Bus.Node;
using Foothill.Persistent.Command;
using Foothill.Bus;
using Foothill.Bus.Result;
using Foothill.Utils;
using GGFW.Entity.SystemManage;

namespace GGFW.DataAccess.SystemManage
{
    public class FoothillMenuDao : IDisposable
    {
        private const string SELECTSQL = "Select * From FoothillMenu Where 1=1 ";
        /// <summary>
        ///根据用户类型获取功能菜单
        /// </summary>
        /// <param name="userID">用户ID</param>
        /// <returns>符合条件的功能菜单</returns>
        public List<FoothillMenu> GetFunctions(int userID)
        {
            string sql = SELECTSQL + " And (MenuID IN (SELECT MenuID FROM dbo.FoothillUserMenu Where UserID={0}) or MenuID IN (SELECT MenuID FROM dbo.FoothillRoleMenu Where RoleID in (select RoleID from dbo.FoothillUserRole Where UserID={0}))) order by MenuID ";
            sql = string.Format(sql, userID);

            NativeSqlBus bus = new NativeSqlBus("FoothillMenu", sql);
            QueryNativeSqlCommand cmd = new QueryNativeSqlCommand(bus);
            ResultQueryNativeSql result = cmd.execute() as ResultQueryNativeSql;

            if (result == null) throw new Exception(GGFW.DataAccess.Constants.Common.UNKNOW_DB_ERROR);
            if (!result.isSuccess()) throw new Exception(result.getMessage());

            List<FoothillMenu> list = new List<FoothillMenu>();
            AbstractResultBus.CopyProperties<FoothillMenu>(list, result.Records, null);
            return list;
        }

        /// <summary>
        /// 获得所有菜单,按排序排序
        /// </summary>
        /// <returns>部门列表</returns>
        public List<FoothillMenu> GetAll()
        {
            //设置查询部门总线
            QueryEntityBus queryBus = new QueryEntityBus("FoothillMenu");
            queryBus.addOrderField(new OrderFieldBus("OrderID", OrderByType.ASC));

            //执行查询部门命令
            QueryEntityCommand queryCommand = new QueryEntityCommand(queryBus);
            ResultQueryEntity result = queryCommand.execute() as ResultQueryEntity;

            //执行结果为空
            if (result == null)
            {
                throw new Exception(Constants.Common.UNKNOW_DB_ERROR);
            }

            //执行命令不成功
            if (!result.isSuccess())
            {
                throw new Exception(result.getMessage());
            }

            //是否存在记录
            if (result.Records != null && result.Records.Count > 0)
            {
                List<FoothillMenu> bizCodeList = new List<FoothillMenu>();
                AbstractResultBus.CopyProperties<FoothillMenu>(bizCodeList, result);

                return bizCodeList;
            }

            return null;
        }

        /// <summary>
        /// 根据角色具有权限的功能菜单数据
        /// </summary>
        /// <param name="RoleID">角色ID</param>
        /// <returns>操作结果</returns>
        public List<FoothillMenu> GetFoothillMenuByRoleID(int RoleID)
        {
            string sql = SELECTSQL + " And MenuID IN (SELECT MenuID FROM dbo.FoothillRoleMenu Where RoleID={0}) order by MenuID ";
            sql = string.Format(sql, RoleID);

            NativeSqlBus bus = new NativeSqlBus("FoothillMenu", sql);
            QueryNativeSqlCommand cmd = new QueryNativeSqlCommand(bus);
            ResultQueryNativeSql result = cmd.execute() as ResultQueryNativeSql;

            if (result == null) throw new Exception(GGFW.DataAccess.Constants.Common.UNKNOW_DB_ERROR);
            if (!result.isSuccess()) throw new Exception(result.getMessage());

            List<FoothillMenu> list = new List<FoothillMenu>();
            AbstractResultBus.CopyProperties<FoothillMenu>(list, result.Records, null);
            return list;
        }

        /// <summary>
        /// 根据角色具有权限的功能菜单数据
        /// </summary>
        /// <param name="RoleID">角色ID</param>
        /// <returns>操作结果</returns>
        public List<FoothillMenu> GetFoothillMenuByUserID(int UserID)
        {
            string sql = SELECTSQL + " And MenuID IN (SELECT MenuID FROM dbo.FoothillUserMenu Where UserID={0}) order by MenuID ";
            sql = string.Format(sql, UserID);

            NativeSqlBus bus = new NativeSqlBus("FoothillMenu", sql);
            QueryNativeSqlCommand cmd = new QueryNativeSqlCommand(bus);
            ResultQueryNativeSql result = cmd.execute() as ResultQueryNativeSql;

            if (result == null) throw new Exception(GGFW.DataAccess.Constants.Common.UNKNOW_DB_ERROR);
            if (!result.isSuccess()) throw new Exception(result.getMessage());

            List<FoothillMenu> list = new List<FoothillMenu>();
            AbstractResultBus.CopyProperties<FoothillMenu>(list, result.Records, null);
            return list;
        }

        /// <summary>
        /// 设置角色菜单
        /// </summary>
        /// <param name="MenuID">菜单ID</param>
        /// <param name="RoleIDs">角色ID</param>
        /// <returns>受影响的记录数</returns>
        public int SetMenuRole(string MenuIDs, string RoleID)
        {
            //删除SQL
            string sql = string.Format("delete from {0} where RoleID ={1};", "FoothillRoleMenu", RoleID);
            //添加SQL
            string[] MenuID = MenuIDs.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < MenuID.Length; i++)
            {
                sql += string.Format("Insert into [{0}] ([MenuID],[RoleID]) Values ({1},{2});", "FoothillRoleMenu", MenuID[i], RoleID);
            }
            ResultNativeSqlDeleteUpdate result = new NativeSqlCommand(new NativeSqlBus("SetMenuRole", sql)).execute() as ResultNativeSqlDeleteUpdate;
            //执行结果为空
            if (result == null)
            {
                throw new Exception(Constants.Common.UNKNOW_DB_ERROR);
            }
            //执行命令不成功
            if (!result.isSuccess())
            {
                throw new Exception(result.getMessage());
            }
            return result.AffectRows;

        }

        /// <summary>
        /// 设置用户菜单
        /// </summary>
        /// <param name="MenuID">菜单ID</param>
        /// <param name="RoleIDs">角色ID</param>
        /// <returns>受影响的记录数</returns>
        public int SetMenuUser(string MenuIDs, string UserID)
        {
            //删除SQL
            string sql = string.Format("delete from {0} where UserID ={1};", "FoothillUserMenu", UserID);
            //添加SQL
            string[] MenuID = MenuIDs.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < MenuID.Length; i++)
            {
                sql += string.Format("Insert into [{0}] ([MenuID],[UserID]) Values ({1},{2});", "FoothillUserMenu", MenuID[i], UserID);
            }
            ResultNativeSqlDeleteUpdate result = new NativeSqlCommand(new NativeSqlBus("SetMenuUser", sql)).execute() as ResultNativeSqlDeleteUpdate;
            //执行结果为空
            if (result == null)
            {
                throw new Exception(Constants.Common.UNKNOW_DB_ERROR);
            }
            //执行命令不成功
            if (!result.isSuccess())
            {
                throw new Exception(result.getMessage());
            }
            return result.AffectRows;

        }

        #region IDisposable 成员

        public void Dispose()
        {
        }

        #endregion
    }
}
